<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <!--<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>-->
            </div>
        </script>

        <table class="layui-hide lay-even" id="currentTableId" lay-filter="currentTableFilter">
        </table>

        <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs data-count-delete" lay-event="delete">删除</a>
                <a class="layui-btn layui-btn-checked layui-btn-xs data-count-rule" lay-event="rule">规则</a>
                <a class="layui-btn layui-btn-default layui-btn-xs data-count-rule" lay-event="price">套餐</a>
                <a class="layui-btn layui-btn-warm layui-btn-xs data-count-generate" lay-event="group">组别</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-generate" lay-event="generate">生成</a>
                <a class="layui-btn layui-btn-checked layui-btn-xs data-count-generate" lay-event="patch">批量号码布</a>
        </script>
    </div>
</div>

<script>

    var selected = {};
    layui.use(['form', 'table', 'miniPage', 'element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            url: '/event/information/list/page',
            method: "post",
            dataType: "json",
            contentType: "application/json",
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'limit' //每页数据量的参数名，默认：limit
            },
            cols: [[
                // {type: "checkbox", minWidth: 30},
                // {field: 'id', title: 'ID', minWidth: 30, sort: true},
                {field: 'name', title: '赛事名称', minWidth: 160, sort: true},
                {field: 'place', title: '赛事地点', minWidth: 160, sort: true},
                {field: 'partBeginTime', title: '报名开始时间', minWidth: 160, sort: true},
                {field: 'partEndTime', title: '报名结束时间', minWidth: 160, sort: true},
                {field: 'statusCode', title: '赛事状态', minWidth: 100, align: "center", sort: true},
                {title: '操作', toolbar: '#currentTableBar', align: "center", minWidth: 400,fixed: 'right'}
            ]],
            done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                // console.log(res);

                //得到当前页码
                // console.log(curr);

                //得到数据总量
                // console.log(count);
            },
            parseData: function (res) { //res 即为原始返回的数据

                let list = [] = res.data.list;

                list.forEach(d => {
                    if (d.status === 1) {
                        d.statusCode = "<span style='color: green'>有效</span>"
                    } else {
                        d.statusCode = "<span style='color: red'>无效</span>"
                    }
                })
                return {
                    "code": 0, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.count, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            },
            initSort: {
                field: 'partBeginTime' //排序字段，对应 cols 设定的各字段名
                , type: 'desc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
            },
            limits: [10, 20, 50, 100000000],
            limit: 10,
            page: true,
            skin: 'line'
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/event/add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加赛事',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function () {
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                        }, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            selected = obj
        });

        table.on('tool(currentTableFilter)', function (obj) {

            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('page/event/edit.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑赛事',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    success: function () {
                        $("#update_id").val(obj.data.id)
                        $("#update_name").val(obj.data.name)
                        $("#update_eventRelationPerson").val(obj.data.eventRelationPerson)
                        $("#update_executive").val(obj.data.executive)
                        $("#update_guide").val(obj.data.guide)
                        $("#update_detail").val(obj.data.detail)
                        $("#update_organizerId").val(obj.data.organizerId)

                        $("#update_host").val(obj.data.host)
                        $("#update_introduction").val(obj.data.introduction)
                        $("#update_partBeginTime").val(obj.data.partBeginTime)
                        $("#update_partEndTime").val(obj.data.partEndTime)
                        $("#update_place").val(obj.data.place)
                        $("#update_raceBeginTime").val(obj.data.raceBeginTime)
                        $("#update_raceEndTime").val(obj.data.raceEndTime)
                        $("#update_sponsor").val(obj.data.sponsor)
                        $("#update_target").val(obj.data.target)
                        $("#update_detail").text(obj.data.detail)
                        $("#pic_show").attr("src",obj.data.pic)
                    },
                    end: function () {
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                        }, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                    $.ajax({
                        url: "/event/information/delete/byId?eventId=" + obj.data.id,
                        method: "post",
                        contentType: "application/json",
                        dataType: "json",
                        success: function (res) {
                            if (res.status === 1) {
                                layer.msg('成功', {
                                    icon: 1,
                                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                                });
                            } else {
                                layer.msg('失败', {
                                    icon: 2,
                                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                                });
                            }
                            form.render();
                        }
                    })

                });
            }
            else if (obj.event === 'generate') {
                $.ajax({
                    url: "/generate/part/page/" + obj.data.id,
                    method: "GET",
                    contentType: "application/json",
                    dataType: "json",
                    success: function (res) {
                        if (res.status === 1) {
                            layer.alert("成功", {icon: 1});
                            window.open("/event" + obj.data.id + "/qrcode.png", '_blank')
                        } else {
                            layer.alert("失败", {icon: 2});
                        }
                        return false;

                    }
                })
            } else if (obj.event === 'rule') {
                var content = miniPage.getHrefContent('page/event/rule.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '赛事规则',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    success: function (layero, index) {
                        $("#edit_info_id").val(obj.data.id)
                    }
                });
            }
            else if (obj.event === 'group') {
                var content = miniPage.getHrefContent('page/event/group.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '赛事组别',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    success: function (layero, index) {
                        $("#group_info_id").val(obj.data.id)
                    }
                });
            }
            else if (obj.event === 'price') {
                var content = miniPage.getHrefContent('page/event/price.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '赛事套餐',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    success: function (layero, index) {
                        $("#price_info_id").val(obj.data.id)
                    }
                });
            } else if (obj.event === 'patch') {
                var content = miniPage.getHrefContent('page/event/patch.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '号码布批量下载',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    success: function (layero, index) {
                        $("#patch_info_id").val(obj.data.id)
                        $("#patch_event_name").val(obj.data.name)
                    }
                });
            }

        });

    });
</script>